package org.javarosa.cdctz.util;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import org.javarosa.core.Context;
import org.javarosa.core.JavaRosaServiceProvider;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.ICommand;
import org.javarosa.core.api.IShell;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.patient.model.Patient;
import org.javarosa.patient.storage.PatientRMSUtility;

public class TempPatientRegActivity implements IActivity, CommandListener {


	TempPatientRegistration reg;
	private IShell parent = null;
	
	Context context;
	
	Patient patient;

	
	public TempPatientRegActivity(IShell parent) {
		this.parent = parent;
	}

	public void contextChanged(Context globalContext) {
		// TODO Auto-generated method stub

	}

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void halt() {
		// TODO Auto-generated method stub

	}

	public void resume(Context globalContext) {
		// TODO Auto-generated method stub

	}
	

	public void start(Context context) {
		reg = new TempPatientRegistration("");
		reg.setCommandListener(this);
		this.context = context;
		Integer patientId = (Integer)this.context.getElement("PATIENT_ID_KEY");
		if(patientId != null) {
			PatientRMSUtility utility = (PatientRMSUtility)JavaRosaServiceProvider.instance().getStorageManager().getRMSStorageProvider().getUtility(PatientRMSUtility.getUtilityName());
			patient = new Patient();
			try {
				utility.retrieveFromRMS(patientId.intValue(), patient);
				reg.setPatient(patient);
			} catch (IOException e) {
				patient = null;
				e.printStackTrace();
			} catch (DeserializationException e) {
				patient = null;
				e.printStackTrace();
			}
		}
		parent.setDisplay(this, reg);
	}

	
	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command arg0, Displayable arg1) {
		Patient patient = reg.getPatient();
		PatientRMSUtility utility = (PatientRMSUtility)JavaRosaServiceProvider.instance().getStorageManager().getRMSStorageProvider().getUtility(PatientRMSUtility.getUtilityName());
		if(this.patient == null) {
			utility.writeToRMS(patient);
		} else {
			this.patient = patient;
			utility.updateToRMS(patient.getRecordId(), patient);
		}
		parent.returnFromActivity(this, Constants.ACTIVITY_COMPLETE, null);
	}
	public Context getActivityContext() {
		return context;
	}
	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.api.IActivity#setShell(org.javarosa.core.api.IShell)
	 */
	public void setShell(IShell shell) {
		this.parent = shell;
	}
	/* (non-Javadoc)
	 * @see org.javarosa.core.api.IActivity#annotateCommand(org.javarosa.core.api.ICommand)
	 */
	public void annotateCommand(ICommand command) {
		throw new RuntimeException("The Activity Class " + this.getClass().getName() + " Does Not Yet Implement the annotateCommand Interface Method. Please Implement It.");
	}
}
